﻿using CPPEI.Coolzon.Designer.UI;
using CPPEI.Coolzon.Designer.Views;
using CPPEI.Coolzon.Designer.Event;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CPPEI.Coolzon.Designer.Event.UI;

namespace CPPEI.Coolzon.Designer.Shell
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            InitMainView();
            
            LoginView loginView = new LoginView();
            bool? ret = loginView.ShowDialog();
            if (ret.HasValue && ret.Value != true)
            {
                this.Close();
            }

            WorkSpaceSelectView workSpaceSelect = new WorkSpaceSelectView();
            ret = workSpaceSelect.ShowDialog();
            if (ret.HasValue && ret.Value != true)
            {
                this.Close();
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            UIEventBus.Register(this);
            UICommandManager.Register(this);
            UIPartCommandManager.Register(this);
        }

        private void LoginView_Close(Coolzon.UI.View view, bool isCancel)
        {
            if (!isCancel)
            {

            }
        }

        void MainWindow_Closed(object sender, EventArgs e)
        {
            UIEventBus.Unregister(this);
        }

        void view_Close(Coolzon.UI.View view, bool isCancel)
        {
            this.Close();
        }

        private void InitMainView()
        {
            MainView view = new MainView();
            UIContext.Current.MainView = view;
            view.Close += view_Close;
            this.Content = view;
        }

        [Common.Subscribe]
        public void OnInputBinding(UIInputBindingEvent @event)
        {
            KeyGestureConverter converter = new KeyGestureConverter();
            KeyGesture keyGesture = (KeyGesture)converter.ConvertFromString(@event.Key);
            this.InputBindings.Add(new KeyBinding(@event.Command, keyGesture));
        }

        /// <summary>
        /// 切换工作空间
        /// </summary>
        /// <param name="event"></param>
        [Common.Subscribe]
        public void OnSwitchWorkSpace(UISwitchWorkSpaceEvent @event)
        {
            this.Dispatcher.Invoke(() =>
            {
                if (UIContext.Current.MainView != null)
                {
                    UIContext.Current.MainView.Destory();
                }
                InitMainView();
            });
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (SolutionTreeViewNodeHelper.IsCancelSaveOrUnSaveCurrentSceneOperation())
            {
                e.Cancel = true;
                return;
            }
            else if (!(UIContext.Current.MainView as MainView).OnSaveLayout())
            {
                e.Cancel = true;
                return;
            }
        }

        [Common.Subscribe]
        public void WindowClose(UIMainWindowCloseEvent @event)
        {
            this.Close();
        }
    }
}
